// mircoApp/idShop/integral_list/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    titlePage: "",
    show: false,
    urlstr: "",
    detail_Url: "",
    detail_json: {},
    detail_list: [],
    list: [],
    topType: 1,
    topStatistic: {},
    count_total: 0, //总分红份数
    nowTimeDate: "",

    isLoadMore: true,
    page: 1,
    total_page: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.type) {
      this.setData({
        type: options.type
      })
      if (options.type == "v_point") {
        this.getTopStatic();
        this.data.urlstr = app.getNetAddresss("plugin.project-party.frontend.controllers.index.vPoint");
        this.data.detail_Url = app.getNetAddresss("plugin.project-party.frontend.controllers.index.vPointDetail");
        this.data.detail_json = {
          dividend_code: ""
        };
      } else if (options.type == "cloud_point") {
        this.getTopStatic();
        this.data.urlstr = app.getNetAddresss("plugin.project-party.frontend.controllers.index.inventPointList");
        this.data.detail_Url = app.getNetAddresss("plugin.project-party.frontend.controllers.index.inventPointDetail");
        this.data.detail_json = {
          order_time: ""
        };
      } else if (options.type == "total_dividend") {
        this.data.titlePage = "总分红份数";
        this.data.urlstr = app.getNetAddresss("plugin.project-party.frontend.controllers.index.memberDividendCount");
        this.setData({
          nowTimeDate: this.formatTimeTwo(Date.parse(new Date()), 'Y-M-D')
        })
        wx.setNavigationBarTitle({
          title: this.data.titlePage,
        })
      } else if (options.type == "middleground") {
        this.data.titlePage = "中台基金";
        this.data.urlstr = app.getNetAddresss("plugin.project-party.frontend.controllers.index.fundList");
        this.data.detail_Url = app.getNetAddresss("plugin.project-party.frontend.controllers.index.fundDetail");
        this.data.detail_json = {
          settle_time: ""
        };
        wx.setNavigationBarTitle({
          title: this.data.titlePage,
        })
      } else if (options.type == "today_dividend") {
        this.data.titlePage = "今日分红";
        this.data.urlstr = app.getNetAddresss("plugin.project-party.frontend.controllers.index.bonusLog");
        this.data.detail_Url = app.getNetAddresss("plugin.project-party.frontend.controllers.index.bonusLogDetail");
        this.data.detail_json = {
          id: ""
        };
        wx.setNavigationBarTitle({
          title: this.data.titlePage,
        })
      }
    }

    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      console.log('dddddddddddddddddd')
      this.getMoreData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  async getMoreData() {
    if (!this.data.isLoadMore) return
    this.data.isLoadMore = false;
    if (this.data.page >= this.data.total_page) {
      return
    } else {
      this.setData({
        page: this.data.page + 1
      })
      let json = {
        page: this.data.page
      }
      let {
        data
      } = await app._postNetWork({
        url: this.data.urlstr,
        data: json
      })
      this.data.isLoadMore = true;
      if (data.result) {
        this.setData({
          list: this.data.list.concat(data.data.data)
        })
      } else {
        app.tips(data.msg);
      }
    }
  },
  tabChange(e) {
    console.log(e)
    this.setData({
      active: e.detail.index
    })
    if (this.data.type == 'v_point') {
      this.setData({
        list: [],
        page: 1,
        total_page: 0,
        isLoadMore: true
      })
      if (e.detail.index == 1) {
        this.data.urlstr = app.getNetAddresss("plugin.project-party.frontend.controllers.index.vPointExpend");
      } else {
        this.data.urlstr = app.getNetAddresss("plugin.project-party.frontend.controllers.index.vPoint");
      }
      this.getData();
    }
  },
  showClose() {
    this.setData({
      show: false
    })
  },
  showDetailPopup(e) {
    let {
      item
    } = e.currentTarget.dataset;
    if (this.data.type == "v_point") {
      this.data.detail_json.dividend_code = item.dividend_code;
    } else if (this.data.type == "today_dividend") {
      this.data.detail_json.id = item.dividend_id;
    } else if (this.data.type == "middleground") {
      this.data.detail_json.settle_time = item.settle_time;
    } else {
      this.data.detail_json.order_time = item.order_time;
    }

    this.getDetail();
  },
  async getDetail() {
    let {
      data
    } = await app._postNetWork({
      url: this.data.detail_Url,
      data: this.data.detail_json
    })
    if (data.result) {
      this.setData({
        detail_list: data.data.data,
        show: true
      })
    } else {
      app.tips(data.msg)
    }
  },
  async getData() {
    let {
      data
    } = await app._postNetWork({
      url: this.data.urlstr
    })
    if (data.result) {
      if (this.data.type == "total_dividend") {
        this.setData({
          list: data.data.data.count_statistic_list,
          count_total: data.data.data.dividend_count_total,
          total_page: 0,
          isLoadMore: false
        })
        console.log(this.data.list,'qqqqqqqqqqqqqqqq')
        return;
      }
      if (this.data.type == "middleground") {
        this.setData({
          count_total: data.data.dividend_value
        })
      }
      if (this.data.type == "today_dividend") {
        this.setData({
          count_total: data.data.dividend_price
        })
      }
      this.setData({
        list: data.data.data,
        total_page: data.data.pages,
        isLoadMore: true
      })
    } else {
      app.tips(data.msg);
    }
  },
  async getTopStatic() {
    switch (this.data.type) {
      case "v_point":
        this.setData({
          topType: 4
        })
        break;
      case "cloud_point":
        this.setData({
          topType: 3
        })
        break;
      default:
        this.setData({
          topType: 4
        })
        break;
    }
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.project-party.frontend.controllers.index.topStatistic"),
      data: {
        point_type: this.data.topType
      }
    })
    if (data.result) {
      this.setData({
        topStatistic: data.data
      })
      this.data.titlePage = `我的${this.data.topStatistic.type_desc}`;
      wx.setNavigationBarTitle({
        title: this.data.titlePage,
      })
    } else {
      app.tips(data.msg)
    }
  },
  formatTimeTwo(number, format) {
    var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
    var returnArr = [];
    var date = new Date(number);
    returnArr.push(date.getFullYear());
    returnArr.push(this.formatNumber(date.getMonth() + 1));
    returnArr.push(this.formatNumber(date.getDate()));
    returnArr.push(this.formatNumber(date.getHours()));
    returnArr.push(this.formatNumber(date.getMinutes()));
    returnArr.push(this.formatNumber(date.getSeconds()));
    for (var i in returnArr) {
      format = format.replace(formateArr[i], returnArr[i]);
    }
    return format;
  },
  formatNumber(n) {
    n = n.toString()
    return n[1] ? n : '0' + n
  }
})